//
//  CNavigationBar.swift
//  CViews
//
//  Created by xhl_dabaoMac on 2021/11/12.
//

import UIKit
import Kingfisher

///状态栏的高度
let statusHeight = UIApplication.shared.statusBarFrame.size.height


public class CNavigationBar: UIImageView {

    ///背景
    public var BGNetImageUrls : String?{
        didSet{
            kf.setImage(with: URL(string: BGNetImageUrls ?? ""))
        }
    }
    public var contentView : UIView = UIView()
    
    
    public override init(frame: CGRect) {
        
        super.init(frame: frame)
        isUserInteractionEnabled = true
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.height.equalTo(44)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        debugPrint("deinit:\(self)")
    }
}

extension CNavigationBar{
    
    /// 添加返回键
    /// - Parameters:
    ///   - view: 要添加的view
    ///   - action: view的点击事件
    public func addLeftView(_ view:UIView,sideSpace:CGFloat = 0,action:(()-> Void)?) {
        contentView.layoutSingleView(view: view,sideSpace:sideSpace, action: action)
    }
    
    /// 添加右上角更多
    /// - Parameters:
    ///   - view: 要添加的view
    ///   - action: view的点击事件
    public func addRightView(_ view:UIView,sideSpace:CGFloat = 0,action:(()-> Void)?) {
        contentView.layoutSingleView(location: .right, view: view,sideSpace:sideSpace, action: action)
    }
    
    /// 添加导航标题
    /// - Parameter title: 标题
    public func addTitle(_ title:String) {
        let view = Creator.getlabel(title:title)
        addCenterView(view, action: nil)
    }
    
    /// 添加导航中部view
    /// - Parameters:
    ///   - view: 要添加的view
    ///   - action: view的点击事件
    public func addCenterView(_ view:UIView ,action:(()-> Void)?) {
        contentView.layoutSingleView(location: .center, view: view, action: action)
    }
    
    /// 添加多个view
    /// - Parameters:
    ///   - startlocation: 从那边开始布局
    ///   - views: 数组view
    ///   - action: item的 事件
    public func addViews(startlocation : CLocation ,
                         views : [UIView],
                         action : @escaping (Int) -> () ) {
        if startlocation == .center {
            
            //先用一个container ，包一下子View，在居中
            let container = UIView()
            container.layoutMultipleView(startlocation: .center, views: views, action: action)
            contentView.layoutSingleView(location: .center, view: container, action: nil)
        }else{
            contentView.layoutMultipleView(startlocation: startlocation, views: views, action: action)
        }
    }
}
